package com.sduept.nwld.primaryequipment.rest;

import com.sduept.core.dao.SuperDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 定时服务管理 系统中需要的定时服务可在此类中实现（比如数据库备份，垃圾数据清理等等）
 * 
 */
@Slf4j
@Service
public class BreakerLastTripTimerService {
	@Autowired
	private SuperDAO dao;

	// /**
	// * 每日凌晨1点开始清理系统消息
	// * 找到最小系统消息id的pad，删除系统消息
	// */
	// @Schedule(dayOfWeek="*",hour="1", minute="1",second="1",persistent=false)
	// public void deleteData(){
	// Enumeration<RegisteredPad> mpads =
	// mem.getClientCurrentStatus().elements();
	// while(mpads.hasMoreElements()){
	// RegisteredPad p = mpads.nextElement();
	// dao.update(p, RegisteredPad.class);
	// }
	//
	// List<RegisteredPad> pads =
	// dao.findByCondition("mid is not null order by mid", RegisteredPad.class);
	// if(pads.size() > 0){
	// long mid = pads.get(0).getMid();
	// if(mid > 0){
	// dao.executeUpdate("delete SystemMessage where id < '"+mid+"'");
	// mem.reloadSystemMessages();
	// }
	// }
	// }
//	@Schedule(dayOfWeek="*",hour="1", minute="1",second="1",persistent=false)
	public void updateBreakerLastTrip() {
		log.info("定时器执行了！！！");
			String sql1 = "select t1.name,t1.sub_name,t1.last_tripatime,t1.last_tripbtime,t1.last_tripctime from t_cim_breaker t1 left join t_cim_substation t2 on t1.sub_id = t2.id where t2.voltageLevel > 499;";
			String sql2 = "select t.cim_soekg,t.cim_soesj,t.cim_bxzbh_event,t.cim_xxqk from t_oms_soe t";
			String sql4="";
			List<?> obs1 = dao.nativeQuery(sql1);
			List<?> obs2 = dao.nativeQuery(sql2);					
			for (int i = 0; i < obs1.size(); i++) {	
				for (int j = 0; j < obs2.size(); j++) {
					Object[] obj1 = (Object[]) obs1.get(i);
					Object[] obj2 = (Object[]) obs2.get(i);	
					 if (obj1[0]==obj2[0]&&obj1[1]==obj2[3]) {
						 log.info("有相同的数据！！！！！！！！");
						if(obj2[2].toString().indexOf("跳闸A")!=-1){
						  sql4 = "update t_cim_breaker t set t.last_tripatime='"
		                            + obj2[1] + "' where t.name='" + obj1[0] + "' and t.cim_xxqk='"+obj1[1]+"'";
						}
						if(obj2[2].toString().indexOf("跳闸B")!=-1){
							sql4 = "update t_cim_breaker t set t.last_tripbtime='"
                                 + obj2[1] + "' where t.name='" + obj1[0] + "' and t.cim_xxqk='"+obj1[1]+"'";
		                }
						if(obj2[2].toString().indexOf("跳闸C")!=-1){
		                	sql4 = "update t_cim_breaker t set t.last_tripctime='"
                                 + obj2[1] + "' where t.name='" + obj1[0] + "' and t.cim_xxqk='"+obj1[1]+"'";                 
	                    }								   				    	
						dao.nativeUpdate(sql4);
					}else{
						log.info("没有结果");
					}
				}
			}
	}
}
